<?php
/**
 * licenses : MIT
 * creatTime: 2020/5/17
 * author : weblinuxgame
 * filename: UserProfileController.php
 */

namespace App\Http\Controllers\JsonApi;

use App\Core\Utils\Message;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Api\Modules\User\UserApi;
use App\Repositories\UserProfileRepository;
use App\Http\Controllers\JsonApiController;

/**
 * Class UserProfileController
 * @package App\Http\Controllers\JsonApi
 */
class UserProfileController extends JsonApiController implements UserApi
{

    /**
     * 获取用户属性更新逻辑服务
     * @return UserProfileRepository
     */
    protected function repository()
    {
        return new UserProfileRepository();
    }

    /**
     * 获取当前用户信息数据
     * @param Request $request
     * @return Response
     */
    public function profile(Request $request): Response
    {
        $uid = $request->uid;
        $keys = $request->get('profiles');
        if (empty($keys)) {
            return $this->response($this->repository()->profiles($uid));
        }
        $keys = explode(',', $keys);
        return $this->response($this->repository()->profiles($uid, $keys));
    }

    /**
     * 更新用户信息
     * @param Request $request
     * @return Response
     */
    public function update(Request $request): Response
    {
        $key = $request->get('key');
        $value = $request->get('value');
        $uid = $request->uid;
        if (empty($avatarId) || empty($uid)) {
            return $this->failed(Message::message(Message::MissRequiredParam), Message::MissRequiredParam);
        }
        return $this->response($this->repository()->update($uid, [$key => $value]));

    }

    /**
     * 更新用户头像
     * @param Request $request
     * @return Response
     */
    public function avatar(Request $request): Response
    {
        $uid = $request->uid;
        $avatarId = $request->get('avatar_id');
        if (empty($avatarId) || empty($uid)) {
            return $this->failed(Message::message(Message::MissRequiredParam), Message::MissRequiredParam);
        }
        return $this->response($this->repository()->update($uid, ['avatar_id' => $avatarId]));
    }

    /**
     * 更新密码
     * @param Request $request
     * @return Response
     */
    public function password(Request $request): Response
    {
        $uid = $request->uid;
        if (empty($id)) {
            return $this->unAuthorized();
        }
        return $this->response($this->repository()->password($id, $request->get('password')));
    }

    /**
     * @inheritDoc
     */
    public function posts(Request $request): Response
    {
        // TODO: Implement posts() method.
    }


}
